﻿using ArtMath.Animation;
using ArtMath.Component;
using ArtMath.Component.Point;
using ArtMath.Flag;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ArtMath.Window
{
    public partial class AddAnimationWindow : Form
    {
        public string LineSpeed
        {
            get { return tbLineSpeed.Text; }
        }
        public string Acceleration
        {
            get { return tbAcceleration.Text; }
        }
        public int Rate
        {
            get { return (int)nudRate.Value; }
        }
        public bool UseHighPerformanceAnimation
        {
            get { return cbUseHighPerformance.Checked; }
        }
        public Direction AnimationDirection
        {
            get { return rbForward.Checked ? Direction.Forward : Direction.Backward; }
        }
        public void ForEachSelectedPoint(Predicate<PointBase> func)
        {
            if (func == null) return;
            foreach (ListViewItem item in lvAvailablePoints.SelectedItems)
            {
                if (!func(item.Tag as PointBase))
                {
                    break;
                }
            }
        }
        public AddAnimationWindow(SketchPad pad)
        {
            InitializeComponent();
            Flags.Ctrl = false;
            cbUseHighPerformance.Enabled = cbUseHighPerformance.Checked = Flags.HighPerformanceTimerEnabled;
            Load += (object sender, EventArgs e) =>
            {
                HashSet<PointBase> set = new HashSet<PointBase>();
                pad.ForEachExistVisibleObject((Base obj) =>
                {
                    ShapeBase sb = obj as ShapeBase;
                    if (sb != null)
                    {
                        sb.ForEachPoint((PointBase pt) =>
                        {
                            if (pt.Movable && set.Add(pt))
                            {
                                ListViewItem item = new ListViewItem(new string[] { pt.Name, pt.GetInHostsNames(), "(" + pt + ")" })
                                {
                                    Tag = pt
                                };
                                lvAvailablePoints.Items.Add(item);
                            }
                            return true;
                        });
                    }
                    return true;
                });
                if (lvAvailablePoints.Items.Count > 0)
                {
                    lvAvailablePoints.Items[lvAvailablePoints.Items.Count - 1].Selected = true;
                }
            };
        }
        private void lvAvailablePoints_Resize(object sender, EventArgs e)
        {
            int width = lvAvailablePoints.ClientSize.Width - 25;
            lvAvailablePoints.Columns[0].Width = (int)(width * 0.3);
            lvAvailablePoints.Columns[1].Width = (int)(width * 0.4);
            lvAvailablePoints.Columns[2].Width = (int)(width * 0.3);
        }

        private void lvAvailablePoints_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnOK.Enabled = lvAvailablePoints.SelectedItems.Count > 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
    }
}
